from CvPythonExtensions import *

import GerikSpells
from GerikSpells import GerikSpellBase
from GSGroupPolicies import GSAll as GSGroup
from GSHandlerPolicies import GSImmediateNoTarget as GSHandler
from GSResultsPolicies import GSMissionNoTarget as GSResults

import GerikSpellsUtils

gc = CyGlobalContext()
localtext = CyTranslator()

class Burrow(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)
        GSResults.__init__(self)

        self.m_iBurrowResearchNeeded = gc.getInfoTypeForString("PROJECT_ZERG_BURROW")
        self.m_iBurrowResearchBuildingBuiltAt = gc.getProjectInfo(self.m_iBurrowResearchNeeded).getBuildingToBuildAt()
        self.m_iUnitsDontNeedBurrow = ( gc.getInfoTypeForString("UNIT_LURKER"), )
        self.m_szNotValidText = localtext.getText( "TXT_KEY_CIVCRAFT_REQUIRES_RESEARCH_AT", (gc.getProjectInfo(self.m_iBurrowResearchNeeded).getDescription(),
                                                                                             gc.getBuildingInfo(self.m_iBurrowResearchBuildingBuiltAt).getDescription()) )


    def getHelpText(self, pGroup, pPlot):
        bValid = self.canGroupCast(pGroup, pPlot, False)
        if not bValid:
            return self.m_szNotValidText
            
    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not gc.getUnitInfo(pCastingUnit.getUnitType()).canBurrow():
            return False

        if pCastingUnit.isBurrowed():
            return False

        # Show this for all burrowable units.
        if bVisible:
            return True

        # Check if they have the research and that the unit needs it.
        if not gc.getPlayer( pCastingUnit.getOwner() ).hasBuiltProject( self.m_iBurrowResearchNeeded ) and (not pCastingUnit.getUnitType() in self.m_iUnitsDontNeedBurrow):
            return False

        return True
        
    def startSpellResults(self, pCastingGroup, pTarget):
        for pUnit in pCastingGroup.getUnits():
            pUnit.setBurrowed(True)
            pUnit.finishMoves()

        # Don't continue the mission.
        return False

class Unburrow(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)
        GSResults.__init__(self)

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not pCastingUnit.isBurrowed():
            return False

        return True
        
    def startSpellResults(self, pCastingGroup, pTarget):
        for pUnit in pCastingGroup.getUnits():
            pUnit.setBurrowed(False)
            pUnit.finishMoves()

        # Don't continue the mission
        return False


        

                        
            
        
            
            
